<?php

/**
 * Class WebUserController
 */

class WebUserController extends BaseController
{

    public function __construct(){
        parent::__construct();
        $user_info = session_get(UserEnum::USER_INFO_SESSION_KEY);
        $userdata  =UserService::instance()->Userfirst();
        if(!empty($user_info)){
            $salt = $_SERVER['REQUEST_URI'];
            Session::put('weburl',$salt);
//            if($user_info->telephone!=$userdata->telephone){
//                Session::flush();
//                header("Location: /Web/web_useraccount.html"); exit;
//            }
//            if($user_info->password!=$userdata->password){
//                Session::flush();
//                header("Location: /Web/web_useraccount.html"); exit;
//               //echo "<script>location='/Web/web_useraccount.html';</script>";
//            }
            $user_id = session_get(UserEnum::USER_INFO_USER_ID);
            if(!empty($user_id)){
                VerifyService::instance()->UserTicketRed($user_id);
            }
        }else{
            $salt = $_SERVER['REQUEST_URI'];
            Session::put('weburl',$salt);
            header("Location: /Web/web_useraccount.html"); exit;
        }

    }



    /**
     * Web投资记录
     * @return mixed
     */
    public function InvestRecordData()
    {
        $page  =  $this->getParam('page');
        if(empty($page)){
            $page = 1;
        }
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $info = WebUserService::instance()->WebInvestRecord($user_id,$page);
        return $info;

    }

    /**
     *投资记录页面
     */
    public function InvestRecord()
    {
        return $this->view('webview.gpw_account_investment');
    }

    /**
     * Web资金流水
     */
    public function MoneyRecord()
    {

        return $this->view('webview.gpw_account_capitalflow');
    }

    public function CZMoneyRecord()
    {
        return $this->view('webview.gpw_account_capitalflow_cz');
    }

    public function TXMoneyRecord()
    {
        return $this->view('webview.gpw_account_capitalflow_tx');
    }

    public function HKMoneyRecord()
    {
        return $this->view('webview.gpw_account_capitalflow_hk');
    }

    public function TZMoneyRecord()
    {
        return $this->view('webview.gpw_account_capitalflow_tz');
    }



    /**
     * 资金流水替换
     * @return mixed
     */
    public function MoneyRecordAjax()
    {

        $type  =  $this->getParam('type');
        $page  =  $this->getParam('page');

        if(empty($page)){
            $page = 1;
        }
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $info = WebUserService::instance()->WebMoneyRecord($user_id,$type,$page);
        $arr = array();
        foreach($info as $k=>$v)
        {
            $arr[$k] = (json_decode($v->update_after,true));
        }
        $arr2 = array();
        foreach($arr as $k=>$v)
        {
            $arr2[$k] = $v['money_deposit']+$v['money_award']+$v['money_return'];
        }

        foreach($info as $k=>$v)
        {
            if($v->amount<0){
                $v->amount =  number_format($v->amount/100,2);
            }else{
                $v->amount =  '+'.number_format($v->amount/100,2);
            }
            foreach($arr2 as $kk=>$vv){
                if($k==$kk){
                    $v->update_after = number_format($vv/100,2);
                }
            }
            foreach(StatusEnum::type() as $kk=>$vv){
                if($v->type == $kk){
                    $v->type = $vv;
                }
            }
        }
        return $info;
    }


    /**
     * Web我的红包接口
     * xf
     */
    public function WebUserAccountsMyred()
    {
        #逻辑处理
        $paramObj               =          new stdClass();
        $paramObj->page        =          $this->getParam('page');//页数
        try{
            $data= WebUserService::instance()->WebUserAccountsMyred($paramObj->page);
            return $data;
            return $this->view('webview.gpw_account_reward');
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }

    }

    /**
     * Web我的红包页面
     * xf
     */
    public function WebUserAccountsMyredcontent()
    {
            return $this->view('webview.gpw_account_reward');
    }

    /**
     * 我的加息券
     * @return mixed
     */
    public function WebUserAccountsTicket()
    {
        return $this->view('webview.gpw_account_reward_tick');

    }

    /**
     * 我的加息券页面
     */
    public function WebUserAccountsTicketData()
    {
        #逻辑处理
        $paramObj               =          new stdClass();
        $paramObj->page         =          $this->getParam('page');//页数

        $info = WebUserService::instance()->WebUserAccountsTicket($paramObj->page);
        return $info;
    }

    /**
     * @return mixed
     * 个人中心
     */
    public function WebUserAccounts(){
        try{
            if(session_get(UserEnum::USER_INFO_USER_ID)){
                $user_id = session_get(UserEnum::USER_INFO_USER_ID);
            }
            $user_account= WebUserService::instance()->WebUserAccounts($user_id);
            $mecount= WebUserService::instance()->WebMessageCount($user_id);//未读消息的数量
            return $this->view('webview.gpw_account')->with(["account"=>$user_account,"mecount"=>$mecount]);
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }
    }

    /**
     * @return mixed
     * 我的邀请
     */
    public function WebMyInvite(){
        $page = $this->getParam('page');
        #收集信息
        $paramObj                          =          new stdClass();
        $paramObj->page                    =          empty($page)?1:$page;
        try{
            if(session_get(UserEnum::USER_INFO_USER_ID)){
                $user_id = session_get(UserEnum::USER_INFO_USER_ID);
            }else{
                return Redirect::to('/Web/weblogin.html');
            }
            //获取用户邀请码
            $user_invite_code = WebUserService::instance()->UserInfoInvite($user_id);
            //邀请人数
            $invite_count = WebUserService::instance()->InviteCount($user_id);
            //已获奖励
            $invest_amount = UserService::instance()->InviteAmount($user_id);
            $page_size = 30;
            $info = WebUserService::instance()->WebUserInvite($user_id,$page,$page_size);
            $data = [
                'user_invite_code'      => $user_invite_code,
                'invite_count'          => $invite_count,
                'invest_amount'         => $invest_amount,
                'info'                  => $info
            ];
            return $this->view('webview.gpw_account_invite')->with('invite',$data);
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }
    }

    /**
     * @return mixed
     * 我的邀请
     */
    public function WebMyInviteAjax(){
        $page = $this->getParam('page');
        #收集信息
        $paramObj                          =          new stdClass();
        $paramObj->page                    =          empty($page)?1:$page;

        try{
            $user_id = session_get(UserEnum::USER_INFO_USER_ID);
            $page_size = 30;
            $info = WebUserService::instance()->WebUserInvite($user_id,$page,$page_size);
            $data = [
                'info'                  => $info
            ];
            $this->rest->success($data, ErrorCodeEnum::STATUS_SUCCESS, "邀请信息获取成功");
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }
    }

    /**
     * @return mixed
     * 充值
     */
    public function WebUserAccountsRecharge(){
        try{
            if(session_get(UserEnum::USER_INFO_USER_ID)){
                $user_id = session_get(UserEnum::USER_INFO_USER_ID);
            }else{
                return Redirect::to('/Web/weblogin.html');
            }
            $user_accounts_recharge = WebUserService::instance()->UserAccountsRecharge($user_id);
            $bankcard = WebUserService::instance()->GetUserBankCard($user_id);
            return $this->view('webview.gpw_account_recharge')->with(['recharge'=>$user_accounts_recharge,'bankcard'=>$bankcard]);
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }

    }

    /**
     * @return mixed
     * 充值提交
     */
    public function UserDepositRecharge()
    {
        // 获取充值金额
        $paramObj = new stdClass();
        $paramObj->amount = $this->getParam('amount') * 100;
        try{
            if (Request::ajax()) {
                return UserService::instance()->_checkDeposit($paramObj->amount);
            }else {
                $amount = UserService::instance()->UserDepositRecharge($paramObj->amount);
                if($amount){
                    return $this->view('webview.gpw_account_recharge_success')->with('amount',$amount);
                }
            }
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }
    }

    /**
     * @return mixed
     * 提现
     */
    public function WebUserCash(){
        try{
            // 用户资产数据
            if(session_get(UserEnum::USER_INFO_USER_ID)){
                $user_id = session_get(UserEnum::USER_INFO_USER_ID);
            }else{
                return Redirect::to('/Web/weblogin.html');
            }
            $fund     = WebUserService::instance()->GetFund($user_id);
            $bankcard = WebUserService::instance()->GetUserBankCard($user_id);
            $tpldata = [
                'fund'     => $fund,
                'bankcard' => $bankcard,
            ];
            return $this->view('webview.gpw_account_withdraw')->with('tpldata',$tpldata);
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }

    }

    /**
     * @return mixed
     * 提现操作
     */
    public function WebUserWithdrawals()
    {
        // 获取提现金额
        $paramObj = new stdClass();
        $paramObj->amount = $this->getParam('amount') * 100;
        try{
            if (Request::ajax()) {
                return UserService::instance()->_checkCash($paramObj->amount);
            }else {
                $amount = UserService::instance()->UserCashRecharge($paramObj->amount);
                if($amount){
                    return $this->view('webview.gpw_account_wdsub')->with('amount',$amount);
                }
            }
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }
    }

    /**
     * @return mixed
     * ajax请求
     */
    public function GetCashFee()
    {

        // 提现金额
        $paramObj = new stdClass();
        $paramObj->amount = $this->getParam('amount');
        if (Request::ajax()) {
            return UserService::instance()->GetCashFee($paramObj->amount);
        }

    }

    /**
     * @return mixed
     * 资金统计
     */
    public function FundStatistics()
    {
        // 用户资产数据
        if(session_get(UserEnum::USER_INFO_USER_ID)){
            $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        }else{
            return Redirect::to('/Web/weblogin.html');
        }
        $fund_info = WebUserService::instance()->WebUserAccounts($user_id);
        return $this->view('webview.gpw_account_total')->with('info',$fund_info);
    }

    /**
     * @return mixed
     * 个人中心--累计收益
     */
    public function WebCumulative()
    {
        try {
            // 登录验证
            if(session_get(UserEnum::USER_INFO_USER_ID)){
                $user_id = session_get(UserEnum::USER_INFO_USER_ID);
            }else{
                return Redirect::to('/Web/weblogin.html');
            }
            $data = WebUserService::instance()->WebCumulativeProfit($user_id);
            return $this->view('webview.gpw_account_accumulated')->with('info',$data);
        } catch (Exception $e) {
            $this->rest->error($e->getMessage(), $e->getCode(), new stdClass());
        }
    }

    /**
     *web实名认证
     * author:xf
     */
    public  function  WebAuthUserName()
    {
        if (Request::isMethod('POST')) {
            $paramObj                   =          new stdClass();
            $paramObj->name             =          $this->getParam('realname');//真是姓名
            $paramObj->idcard           =          $this->getParam('idcard');//身份证
            $paramObj->banck             =          $this->getParam('banck');//银行卡号
            $paramObj->phone             =          $this->getParam('phone');//手机号
            try{
                $data= WebUserService::instance()->WebAuthUserName($paramObj);
               if($data==1){
                   $mess['code'] = '1';
                   $mess['mess'] = '验证成功';
                   return $mess;
               }
            }catch(Exception $e){
                $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
            }
        }else{
            return $this->view('webview.gpw_relname');
        }
    }
    /**
     *web实名认证成功跳转页面
     * author:xf
     */
    public  function  WebAuthUserNameSucess(){
        return $this->view('webview.gpw_realname_success');
    }
    /**
     *修改密码
     *author:xf
     **/
    public function WebUpdatepwd(){
        if (Request::isMethod('POST')) {
                #接收参数
                if(!get_magic_quotes_gpc()){
                    $paramObj = new stdClass();
                    $paramObj->oldpwd              =addslashes($this->getParam('oldpwd'));
                    $paramObj->newpwd          =addslashes($this->getParam('newpwd'));
                }else{
                    $paramObj = new stdClass();
                    $paramObj->oldpwd               =    $this->getParam('oldpwd');          //密码
                    $paramObj->newpwd            =    $this->getParam('newpwd');      //新密码
                }
            try{
                return  WebUserService::instance()->WebUpdatepwd($paramObj);

            }catch(Exception $e){
                $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
            }
        }else{
            return $this->view('webview.gpw_setpwd');
        }
    }

    /**
     *我的消息
     *author:xf
     **/
    public function WebUserMessage(){
        #逻辑处理
        $paramObj = new stdClass();
        $paramObj->page = $this->getParam('page');//页数
        $paramObj->type = $this->getParam('type');//类型
        try {
            if($paramObj->type=='message'){
                $data = WebUserService::instance()->UserAccountsOwnmesg($paramObj);
                return $data;
            }
            //获取平台公告成功
            if($paramObj->type=='news'){
                $data = WebUserService::instance()->UserAccountsOwnmesg($paramObj);
                return $data;
            }

        } catch (Exception $e) {
            $this->rest->error($e->getMessage(), $e->getCode(), new stdClass());
        }

        return $this->view('webview.gpw_account_notify');
    }

    /**
     *我的消息
     *author:xf
     **/
    public function MyMsg(){
        return $this->view('webview.gpw_account_notify_child');
    }


    /**
     *我的消息--详情
     *author:xf
     **/
    public function WebUserMessageContent(){
        #逻辑处理
           $auto_id = $this->getParam('auto_id');//消息id
           $data= WebUserService::instance()->WebUserMessageContent($auto_id);
           return $this->view('webview.gpw_account_msg')->with('info',$data);
    }

    /**
     *修改我的消息状态
     *author:xf
     **/
    public function WebUserUpdateStaue(){
        #逻辑处理
        $auto_id = $this->getParam('auto_id');//消息id
        try {
            $data = WebUserService::instance()->WebUserUpdateStaue($auto_id);
            return $data;

        } catch (Exception $e) {
            $this->rest->error($e->getMessage(), $e->getCode(), new stdClass());
        }

    }

    /**
     *个人中心--设置
     *author:xf
     **/
    public function WebUserAccountInstall(){
        #实名认证
        $user_id    =session_get(UserEnum::USER_INFO_USER_ID);
        $data=WebUserService::instance()->WebUseruserinfo($user_id);
        $bankcard   =WebUserService::instance()->WebUserBankcard($user_id);
        return $this->view('webview.gpw_account_install')->with(['date'=>$data,'bank'=>$bankcard]);
    }
    /**
     * web意见反馈
     */
    public function WebUserFeedBack(){
        return $this->view('webview.gpw_account_sugg');
    }




    /**
     * 退出登录
     * @return mixed
     */
    public function WebLoginOut()
    {
        try{
            #清空SESSION
            Session::flush();
            return Redirect::to('/Web/web_useraccount.html');
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 解绑银行卡
     * @return mixed
     */
    public function UnBankCard(){
        try {
            $data = WebUserService::instance()->WebUnBankCard();
            $user_info = session_get(UserEnum::USER_INFO_SESSION_KEY);

        } catch (Exception $e) {
            $this->rest->error($e->getMessage(), $e->getCode(), new stdClass());
        }

        return $this->view('webview.gpw_account_unbundcard')->with(["bank"=>$data,"user_info"=>$user_info]);
    }

    /**
     * @return mixed
     * 关于我们
     */
    public function UserAccountAbout(){

        return $this->view('webview.gpw_account_aboutus');

    }
    /**
     * @return mixed
     * 关于钢票
     */
    public function UserAccountAboutDetal(){

        return $this->view('webview.gpw_account_aboutus_detal');

    }

    /**
     * @return mixed
     * 关于钢票
     */
    public function WebUserAccountNewshow(){

        return $this->view('webview.gpw_account_newshow');

    }


}